package ai.people.platform.archives.ws.executor;

import javax.websocket.Session;
import java.io.IOException;

/**
 * @author LiGuanNan
 * @date 2022/7/18 10:43
 */
public class HeartCheckWebSocketExecutor implements WebSocketExecutor {
    /**
     * 连接成功
     *
     * @param session
     */
    @Override
    public void onOpen(Session session) {

    }

    /**
     * 连接关闭
     *
     * @param session
     */
    @Override
    public void onClose(Session session) {

    }

    /**
     * 接收到消息
     *
     * @param text
     * @param session
     */
    @Override
    public String onMsg(String text, Session session) {
        if("ping".equalsIgnoreCase(text)){
            sendMsg("pong", session);
        }
        return "success";
    }

    /**
     * 接收到消息
     *
     * @param text
     * @param session
     */
    @Override
    public String sendMsg(String text, Session session) {
        try {
            session.getBasicRemote().sendText(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
